package sz.szsmk.citizencard.ui.dialogs

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import kotlinx.android.synthetic.main.dialog_verifycode.*
import sz.szsmk.citizencard.R

/**
 * author : clark
 * date : 2021/1/13
 * description :
 */
class VerifyCodeDialog(context: Context, phone: String, url: String) : Dialog(context, R.style.alertdialog) {

    var mContext: Context = context
    var iVerifyCodeDialog: IVerifyCodeDialog? = null
    var myPhone: String = phone
    var codeUrl: String = url
    lateinit var view: View

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        view = LayoutInflater.from(mContext).inflate(R.layout.dialog_verifycode, null)
        setContentView(view)
        val divierId = mContext.resources.getIdentifier("android:id/titleDivider", null, null)
        val divider = findViewById<View>(divierId)
        divider.visibility = View.GONE
        initView()
    }

    private fun initView() {
        //新版本用户协议

        Glide.with(mContext).load(codeUrl)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(graphics_code_img)

        graphics_btn_cancel.setOnClickListener { v: View? ->
            Log.d("clark","cancel")
            dismiss()
        }

        graphics_refresh.setOnClickListener{v:View->
            //刷新图片
            iVerifyCodeDialog?.refresh(myPhone)
        }
        graphics_code_img.setOnClickListener { v:View->
            //刷新图片
            iVerifyCodeDialog?.refresh(myPhone)
        }

        graphics_btn_ok.setOnClickListener { v: View? ->
            Log.d("clark","ok")
            if (iVerifyCodeDialog != null) {
                if (graphics_code_text.length() == 4) {
                    iVerifyCodeDialog?.onclick(myPhone, graphics_code_text.text.toString())
                } else {
                    Toast.makeText(mContext, "请输入图形验证码", Toast.LENGTH_LONG).show()
                }
            }
        }
    }

    //提供接口
      fun setIVerifyCodeDialogNew(iVerifyCodeDialog: IVerifyCodeDialog?) {
        if (iVerifyCodeDialog != null) {
            this.iVerifyCodeDialog = iVerifyCodeDialog
        }
    }

    interface IVerifyCodeDialog {
        fun onclick(phone: String, code: String)
        fun  refresh(phhone:String)
    }

    public fun update(newUrl:String) {
        Glide.with(mContext).load(newUrl)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(graphics_code_img)
        view.invalidate();
    }


}